<template>
  <div>
    <el-card>
      <el-page-header icon="" title="新闻管理">
        <template #content>
          <span class="text-large font-600 mr-3" style="font-size: 16px">
            新闻列表
          </span>
        </template>
      </el-page-header>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="title" label="标题" width="180" />
        <el-table-column prop="category" label="分类">
          <template #default="scope">
            {{ categoryFormat(scope.row.category) }}
          </template>
        </el-table-column>
        <el-table-column prop="editTime" label="更新时间">
          <template #default="scope">
            {{ dayjs(scope.row.editTime).format("YYYY年MM月DD日") }}
          </template>
        </el-table-column>
        <el-table-column prop="isPublish" label="是否发布">
          <template #default="scope">
            <el-switch
              v-model="scope.row.isPublish"
              :active-value="1"
              :inactive-value="0"
              @change="handleSwitchChange(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              circle
              :icon="View"
              type="success"
              @click="handlePreview(scope.row)"
            >
            </el-button>
            <el-button circle :icon="Edit" @click="handleEdit(scope.row)">
            </el-button>
            <el-popconfirm
              title="你确定删除吗"
              cancel-button-text="取消"
              confirm-button-text="确定"
              @confirm="handleDelete(scope.row)"
            >
              <template #reference>
                <el-button circle :icon="Delete" type="danger"> </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        v-model="dialogVisible"
        title="预览新闻"
        width="70%"
        :before-close="handleClose"
      >
        <div>
          <h2>{{ priviewData.title }}</h2>
          <div style="font-size: 12px; color: gray">
            {{ dayjs(priviewData.editTime).format("YYYY年MM月DD日") }}
          </div>
          <el-divider>
            <el-icon><star-filled /></el-icon>
          </el-divider>
          <!-- v-html  需要有完全信任的后端-->
          <div v-html="priviewData.content" class="htmlcontent"></div>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import axios from "axios";
import { dayjs } from "element-plus";
import "dayjs/locale/zh-cn";
import { View, Edit, Delete, StarFilled } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
const router = useRouter();

const tableData = ref([]);
const priviewData = ref({});
const dialogVisible = ref(false);

onMounted(() => {
  // 设置语言为中文
  dayjs.locale("zh-cn");
  getTableData();
});

const getTableData = async () => {
  const res = await axios.get("/adminapi/news/list");
  // console.log(res.data.result.list);

  tableData.value = res.data.result.list;
};

//格式化分类信息
const categoryFormat = (category) => {
  const arr = ["最新动态", "典型案例", "通知公告"];
  return arr[category - 1];
};

//开关回调
const handleSwitchChange = async (item) => {
  await axios.put("/adminapi/news/publish", {
    id: item.id,
    isPublish: item.isPublish,
  });

  getTableData();
};

//预览回调
const handlePreview = (data) => {
  priviewData.value = data;
  dialogVisible.value = true;
};

//删除回调
const handleDelete = async (item) => {
  await axios.delete(`/adminapi/news/list/${item.id}`);
  await getTableData();
};

//编辑回调
const handleEdit = (item) => {
  //跳转到/news-manage/editnews/:id

  router.push(`/news-manage/editnews/${item.id}`);
};
</script>

<style scoped>
.el-table {
  margin-top: 50px;
}
::v-deep .htmlcontent {
  img {
    max-width: 100%;
  }
}
</style>
